//
//  ChainSearchHistoryView.m
//  Chain
//
//  Created by zhaosh on 2018/4/4.
//  Copyright © 2018年 zhaosh. All rights reserved.
//

#import "ChainSearchHistoryView.h"
#import "ChainSearchHistoryFooterView.h"
#import "ChainNewsCell.h"
#import "JHSysAlertUtil.h"
#import "ChainDBManager.h"

@interface ChainSearchHistoryView()<UITableViewDelegate,UITableViewDataSource,ChainSearchHistoryFooterViewDelegate>

@property(strong,nonatomic) UITableView *tableview;
@property(strong,nonatomic) NSMutableArray *dataArr;
@property(strong,nonatomic) ChainSearchHistoryFooterView *footerV;
@end

@implementation ChainSearchHistoryView
-(NSMutableArray *)dataArr{
    if(!_dataArr){
        _dataArr = [NSMutableArray new];
    }
    return _dataArr;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = AllViewBackgroundColor;
        [self setupSubviews];
    }
    return self;
}

-(void)setupSubviews{
    self.dataArr = [[[ChainDBManager shareClient] fetchNewsSearchHistory] mutableCopy];
    self.tableview = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = AllViewBackgroundColor;
    [self addSubview:self.tableview];
 
}

-(void)footerVClearBtnClicked{
    [JHSysAlertUtil presentAlertViewWithTitle:@"提示" message:@"确认清空历史搜索" cancelTitle:@"取消" defaultTitle:@"确定" distinct:NO cancel:^{
        
    } confirm:^{
        [[ChainDBManager shareClient] deleteAllNewsSearchHistory];
        self.dataArr = [[[ChainDBManager shareClient] fetchNewsSearchHistory] mutableCopy];
        [self.tableview reloadData];
        self.footerV.hidden = (self.dataArr.count == 0);
    }];
}

-(void)searchdeleteActionWithCell:(ChainNewsCell *)cell{
    
    NSIndexPath *idp = [self.tableview indexPathForCell:cell];
    ChainSearchHistoryItem *i = self.dataArr[idp.row];
    if ([[ChainDBManager shareClient]deleteNewsSearchHistoryById:i.id_]) {
        [self.dataArr removeObject:i];
        [self.tableview deleteRowsAtIndexPaths:@[idp] withRowAnimation:UITableViewRowAnimationFade];
        
    }
    
    self.footerV.hidden = (self.dataArr.count == 0);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChainNewsCell *cell = [ChainNewsCell cellWithTableView:tableView];
    cell.item = self.dataArr[indexPath.row];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ChainSearchHistoryItem *item = self.dataArr[indexPath.row];
    if (self.selectBlock) {
        self.selectBlock(item);
    }
}

-(void)setListVHidden:(BOOL)hidden{
    self.hidden = hidden;
    self.dataArr = [[[ChainDBManager shareClient] fetchNewsSearchHistory] mutableCopy];
    [self.tableview reloadData];
    self.footerV.hidden = (self.dataArr.count == 0);
}
@end
